package dshop.model;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;

import appbase.jfinal.ext.BaseModel;

public class UserTag extends BaseModel<UserTag> {

	private static final long serialVersionUID = -6967680902549117464L;

	public static final UserTag dao = new UserTag();

	// 1 推送关键词
	public static final int TAG_TYPE_PUSH_TAG = 1;

	/**
	 * @return [{ id, userId, title, totalCnt }]
	 */
	public static List<UserTag> listByHitting(String searchContent, int tagType) {
		String sql = " SELECT ut.id, ut.userId, ut.title, ut.totalCnt FROM user_tag ut LEFT JOIN push_num pu on ut.userId = pu.userId WHERE "
				+ " ut.isDel = 0 AND pu.num > 0 AND pu.switch_push = 1 AND ut.tagType = ? AND locate(title,?)";
		return dao.find(sql, tagType, searchContent);
	}

	public static void plusUseCnt(List<Integer> userTagIdList) {
		if (userTagIdList == null || userTagIdList.isEmpty()) {
			return;
		}

		String sql = " update user_tag set useCnt = useCnt + 1 where id in (??) ";
		sql = dao.buildSqlHasIn(sql, userTagIdList);
		Db.update(sql);
	}

	public static void minusUseCnt(List<Integer> userTagIdList) {
		if (userTagIdList == null || userTagIdList.isEmpty()) {
			return;
		}

		String sql = " update user_tag set useCnt = useCnt - 1 where id in (??) ";
		sql = dao.buildSqlHasIn(sql, userTagIdList);
		Db.update(sql);
	}

}
